﻿using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UI;
public class CreatePanelEditor : EditorWindow
{
    static string panelName;
    private void OnGUI()
    {
        GUILayout.BeginHorizontal();
        GUILayout.Label("请给面板起个NB的名字：", GUILayout.Width(200));
        GUILayout.EndHorizontal();
        panelName = GUILayout.TextField(panelName);
        if (GUILayout.Button("创建Panel"))
        {
            CreatePanel();
            Close();
        }
    }

    private static void CreatePanel()
    {
        GameObject[] objTemp = Selection.gameObjects;
        if (objTemp.Length > 0)
        {
            GameObject Panel = CreateGameObj(panelName, objTemp[0].transform);
            GameObject BG = CreateGameObj("BG", Panel.transform);
            BG.AddComponent<Image>();
            BG.SetActive(false);
            GameObject ItemArea = CreateGameObj("ItemArea", Panel.transform);
            GameObject ButtonGroup = CreateGameObj("ButtonGroup", Panel.transform);
            GameObject Common = CreateGameObj("Common", Panel.transform);
            GameObject Tips = CreateGameObj("Tips", Panel.transform);
        }
        else
        {
            Debug.LogWarning("没有选中物体");
        }
    }

    /// <summary>
    /// 创建物体
    /// </summary>
    /// <param name="objName"></param>
    /// <param name="parent"></param>
    /// <returns></returns>
    private static GameObject CreateGameObj(string objName,Transform parent)
    {
        GameObject obj = new GameObject(objName);
        RectTransform ObjRect = obj.AddComponent<RectTransform>();
        ObjRect.sizeDelta = parent.GetComponent<RectTransform>().sizeDelta;
        obj.transform.parent = parent;
        ObjRect.localPosition = Vector3.zero;
        return obj;
    }

    [MenuItem(itemName:"GameObject/FastCreateUIPanel",priority = 0)]
    static void FastCreatePanel()
    {
        var window = GetWindow<CreatePanelEditor>();
        window.Show();
    }



}
